﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmXuatKho : Form
    {
        private int MaKho;
        private XuatKhoService xkService = new XuatKhoService();
        private ChiTietKhoService CTKhoService = new ChiTietKhoService();
        private DataTable dbNguyenLieu = new DataTable();
        private DataTable dbNguyenLieuXuat = GetTableNguyenLieuXuat();

        private static DataTable GetTableNguyenLieuXuat()
        {
            // Here we create a DataTable with five columns.
            DataTable table = new DataTable();
            table.Columns.Add("MaNL", typeof(int));
            table.Columns.Add("TenNL", typeof(string));
            table.Columns.Add("TenNCC", typeof(string));
            table.Columns.Add("TenDVTinh", typeof(string));
            table.Columns.Add("SoLuong", typeof(float));
            return table;
        }
        public frmXuatKho()
        {
            InitializeComponent();
        }
        public frmXuatKho(int maKho)
        {
            MaKho = maKho;
            InitializeComponent();
        }
        private void frmXuatKho_Load(object sender, EventArgs e)
        {
            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
            LoadThongTinNLKho(dbNguyenLieu);
        }

        public void LoadThongTinNLKho(DataTable dbtab)
        {
            lv_NLKho.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow["MaNL"].ToString();
                liv.SubItems.Add(dbrow["TenNL"].ToString());
                liv.SubItems.Add(dbrow["TenNCC"].ToString());
                liv.SubItems.Add(dbrow["LuongTon"].ToString());
                liv.SubItems.Add(dbrow["SucChuaThapNhat"].ToString());
                liv.SubItems.Add(dbrow["TenDVTinh"].ToString());
                //Tinh Trang
                if (int.Parse(dbrow[5].ToString()) == 0)
                    liv.SubItems.Add("Hết NL");
                if (int.Parse(dbrow[5].ToString()) == 1)
                    liv.SubItems.Add("Sắp Hết NL");
                if (int.Parse(dbrow[5].ToString()) == 2)
                    liv.SubItems.Add("Còn NL");
                lv_NLKho.Items.Add(liv);
            }
        }

        public void LoadNguyenLieuXuat(DataTable dbtab)
        {
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow["MaNL"].ToString();
                liv.SubItems.Add(dbrow["TenNL"].ToString());
                liv.SubItems.Add(dbrow["TenNCC"].ToString());
                liv.SubItems.Add(dbrow["TenDVTinh"].ToString());
                liv.SubItems.Add(dbrow["SoLuong"].ToString());
                lv_NLXuat.Items.Add(liv);
            }
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (lv_NLKho.FocusedItem != null)
            {
                
                if (txtSoLuong.Text == "")
                    return;
                int maNL = int.Parse(lv_NLKho.FocusedItem.Text);
                for (int i = 0; i < lv_NLXuat.Items.Count; i++)
                {
                    if (int.Parse(lv_NLXuat.Items[i].SubItems[0].Text) == maNL)
                    {
                        MessageBox.Show("Nguyên liệu đã có trong mục nhập");
                        return;
                    }
                }
                float SoLuong = float.Parse(txtSoLuong.Text);
                dbNguyenLieuXuat.Rows.Add(lv_NLKho.FocusedItem.SubItems[0].Text, lv_NLKho.FocusedItem.SubItems[1].Text, lv_NLKho.FocusedItem.SubItems[2].Text, lv_NLKho.FocusedItem.SubItems[5].Text, SoLuong);
                lv_NLXuat.Items.Clear();
                LoadNguyenLieuXuat(dbNguyenLieuXuat);

                //cap nhat LV Nguyen Lieu
                //for (int i = 0; i < dbNguyenLieu.Rows.Count; i++)
                //{
                //    if (int.Parse(dbNguyenLieu.Rows[i]["MaNL"].ToString()) == maNL)
                //    {
                //        dbNguyenLieu.Rows[i].Delete();
                //    }
                //}
                //lv_NLKho.Items.Clear();
                //LoadThongTinNLKho(dbNguyenLieu);
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn nguyên liệu");
            }
        }

        private void btnUnMove_Click(object sender, EventArgs e)
        {
            if (lv_NLXuat.FocusedItem != null)
            {
                int maNL = int.Parse(lv_NLXuat.FocusedItem.Text);
                //Cap nhat LV Nguyen Lieu
                //dbNguyenLieu.Rows.Add(lv_NLXuat.FocusedItem.SubItems[0].Text, lv_NLXuat.FocusedItem.SubItems[1].Text, null, lv_NLXuat.FocusedItem.SubItems[2].Text, lv_NLNhap.FocusedItem.SubItems[3].Text);
                //lv_NL.Items.Clear();
                //LoadNguyenLieu(dbNguyenLieu);

                //Cap nhat LV Nguyen Lieu Xuat
                for (int i = 0; i < dbNguyenLieuXuat.Rows.Count; i++)
                {
                    if (int.Parse(dbNguyenLieuXuat.Rows[i]["MaNL"].ToString()) == maNL)
                        dbNguyenLieuXuat.Rows.Remove(dbNguyenLieuXuat.Rows[i]);
                }
                lv_NLXuat.Items.Clear();
                LoadNguyenLieuXuat(dbNguyenLieuXuat);
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn nguyên liệu");
            }
        }

        private void btnXuatKho_Click(object sender, EventArgs e)
        {
            if(lv_NLXuat.Items.Count > 0)
            {
                XuatKho xk = new XuatKho();
                
                xk.CHITIETXUAT = dbNguyenLieuXuat;
                xk.MAKHO = MaKho;
                xk.NGAYXUAT = dtpNgayXuat.Value;
                
                //xuat kho + cap nhat luong ton chitietkho
                if (!fix && !deadlock)
                {
                    if (!xkService.ThemXuatKho(ref xk))
                        MessageBox.Show("Xuất hàng không thành công");
                    else
                    {
                        //Them XuatHang thanh cong. Them ChiTietXuatKho + cap nhat luong ton ChiTietKho
                        //MessageBox.Show(xk.MAXUATKHO.ToString());
                        if (xkService.ThemCTXuatKho(xk))
                        {
                            //them chi tiet XuatKho thanh cong
                            dbNguyenLieuXuat = GetTableNguyenLieuXuat();
                            dbNguyenLieuXuat.Rows.Clear();
                            lv_NLXuat.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            LoadThongTinNLKho(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa XuatKho.
                            xkService.XoaXuatKho(xk.MAXUATKHO);
                            MessageBox.Show("Xuất kho không thành công");
                        }
                    }
                }
                if (!fix && deadlock)
                {
                    if (!xkService.ThemXuatKho(ref xk))
                        MessageBox.Show("Xuất hàng không thành công");
                    else
                    {
                        //Them XuatHang thanh cong. Them ChiTietXuatKho + cap nhat luong ton ChiTietKho
                        //MessageBox.Show(xk.MAXUATKHO.ToString());
                        if (xkService.ThemCTXuatKhoDeadlockError(xk))
                        {
                            //them chi tiet XuatKho thanh cong
                            dbNguyenLieuXuat = GetTableNguyenLieuXuat();
                            dbNguyenLieuXuat.Rows.Clear();
                            lv_NLXuat.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            LoadThongTinNLKho(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa XuatKho.
                            xkService.XoaXuatKho(xk.MAXUATKHO);
                            MessageBox.Show("Xuất kho không thành công");
                        }
                    }
                }

                if (fix && deadlock)
                {
                    if (!xkService.ThemXuatKho(ref xk))
                        MessageBox.Show("Xuất hàng không thành công");
                    else
                    {
                        //Them XuatHang thanh cong. Them ChiTietXuatKho + cap nhat luong ton ChiTietKho
                        //MessageBox.Show(xk.MAXUATKHO.ToString());
                        if (xkService.ThemCTXuatKhoDeadlock(xk))
                        {
                            //them chi tiet XuatKho thanh cong
                            dbNguyenLieuXuat = GetTableNguyenLieuXuat();
                            dbNguyenLieuXuat.Rows.Clear();
                            lv_NLXuat.Items.Clear();
                            dbNguyenLieu = CTKhoService.LayChiTietKho(MaKho);
                            LoadThongTinNLKho(dbNguyenLieu);
                        }
                        else
                        {
                            //them khong thanh cong. Xoa XuatKho.
                            xkService.XoaXuatKho(xk.MAXUATKHO);
                            MessageBox.Show("Xuất kho không thành công");
                        }
                    }
                }
                

            }else
                MessageBox.Show("Không có nguyên liệu trong danh sách");
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dbNguyenLieuXuat = GetTableNguyenLieuXuat();
            lv_NLXuat.Items.Clear();
            txtSoLuong.Text = "";
            DataTable dbtab = CTKhoService.LayChiTietKho(MaKho);
            LoadThongTinNLKho(dbtab);
        }

        private bool fix = false;
        private bool deadlock = false;

        private void cb_deadlock_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_deadlock.Checked)
                deadlock = true;
            else
                deadlock = false;
        }

        private void cb_fix_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_fix.Checked)
                fix = true;
            else
                fix = false;
        }
    }
}
